function ccR_Druid_4()
	if ccrPlayerLevel() == 90 then Druid_Restoration_90v2() end
end

local function HasActiveTotem()
  local active = false
  for i = 1, 4 do
    active = active or GetTotemInfo(i)
  end
  return active
end

function Druid_Restoration_90v2()
	ccRS.Auto_Target = false
	ccRS.Auto_Facing = false
	
function ccrFindDebuffInGroup(debuff)
	local vGroupSize = GetNumGroupMembers()
	local vGroupType = nil
	if IsInRaid() then vGroupType = "raid" else vGroupType = "party" end
	for i = 0, vGroupSize do
		local vPlayerID = vGroupType .. i
		if i == 0 then vPlayerID = "player" end
		if ccrUnitHasDebuff(vPlayerID, debuff, true) then
			return vPlayerID
		end
	end
end		
	
	local vPlayerWithDebuff = ccrFindDebuffInGroup("Mark of Arrogance")
	if vPlayerWithDebuff ~= nil
		and ccrPlayerHasDebuff("Gift of the Titans")
		and ccrSpellIsReady("Nature's Cure")
	then
		ccrCast("Nature's Cure", vPlayerWithDebuff)
		return
	end
	if vPlayerWithDebuff ~= nil
		and ccrTargetName() == "Sha of Pride"
		and ccrTargetHealth() <= 30
		and ccrSpellIsReady("Nature's Cure")
	then
		ccrCast("Nature's Cure", vPlayerWithDebuff)
		return
	end
	--[[
	local vToDispel = ccrFindDebuffInGroup("Mark of Arrogance")
	if vToDispel
		and SpellIsReady("Nature's Cure")
		and ccrPlayerHasDebuff("Gift of the Titans")
	then
		ccrCast("Nature's Cure", vToDispel)
		vToDispel = nil
		return
	end	
	if vToDispel
		and ccrTargetName() == "Sha of Pride"
		and ccrTargetHealth() <= 30
	then
		ccrCast("Nature's Cure", vToDispel)
		vToDispel = nil
		return
	end	
	]]--
	------------------------
	-- TO DO LIST
	-- Heal the blobs on Immersius
	-- Heal and dispel the NPCs on Norushen
	-- Dispel Mark of Arrogance on Sha of Pride when have Gift of the Titans and when Sha have 30% or less health
	-- Stopcasting when Thok is casting Deafening Screech
	-- Cast Iron Bark on self when 6 seconds left of "Iron Prison" debuff
	------------------------
	-- Cast Essential Buffs
	if not ccrPlayerHasBuff("Dash")
	and not ccrPlayerHasBuff("Incarnation: Tree of Life")
	then ccrCastBuffIfMissing("Treant Form") end 
	ccrCastBuffIfMissing("Mark of the Wild")
	----------------------
	if ccrPlayerIsInCombat() then
		if tblTGetNearbyNPC ~= nil
		and #tblTGetNearbyNPC > 0 
		then
			for x = 1, #tblTGetNearbyNPC do
				local vNPC = tblTGetNearbyNPC[x]
				if vNPC
				and vNPC:Exists()
				and not vNPC:IsDead()
				and (
					vNPC:GetName() == "Contaminated Puddle"
					)
				then
					local vNPC_Health = (vNPC:GetHealth()/vNPC:GetMaxHealth()*100)
					if vNPC:GetAura("Lingering Corruption") ~= nil then
						vNPC:Target()
						vNPC:CastSpellByName("Nature's Cure")
					end
					if vNPC_Health < 100 then
						vNPC:Target()
						vNPC:CastSpellByName("Regrowth")
						return
					end
				end
			end
		end
		if tblTGetNearbyNPC ~= nil
		and #tblTGetNearbyNPC > 0 
		then
			for x = 1, #tblTGetNearbyNPC do
				local vNPC = tblTGetNearbyNPC[x]
				if vNPC
				and vNPC:Exists()
				and not vNPC:IsDead()
				and (
					vNPC:GetName() == "Contaminated Puddle"
					or vNPC:GetName() == "Leven Dawnblade"
					or vNPC:GetName() == "Rook Stonetoe"
					or vNPC:GetName() == "Sun Tenderheart"
					)
				then
					local vNPC_Health = (vNPC:GetHealth()/vNPC:GetMaxHealth()*100)
					if vNPC:GetAura("Lingering Corruption") ~= nil then
						vNPC:Target()
						vNPC:CastSpellByName("Nature's Cure")
						return
					end
					if vNPC_Health < 70 then
						vNPC:Target()
						vNPC:CastSpellByName("Wild Growth")
						return
					end
					if vNPC_Health < 90 then
						vNPC:Target()
						vNPC:CastSpellByName("Regrowth")
						return
					end
					if vNPC_Health < 100 then
						vNPC:Target()
						vNPC:CastSpellByName("Rejuvenation")
						return
					end
				end
			end
		end
	end
	--------------------------
	-- Find all nearby players
	Gather_Nearby_Players()
	--------------------------
	-- Autoaccept LFR invite
	AcceptProposal();
	--------------------------
	-- Autoaccept Readycheck
	ConfirmReadyCheck(1)
	--------------------------
	-- Autoaccept Resurrection
	AcceptResurrect();
	--------------------------


	if IsShiftKeyDown()
		then ccrCast("Wild Mushroom")
			 CameraOrSelectOrMoveStart()
			 CameraOrSelectOrMoveStop()
		return
	end

	-- Find who has the lowest health
	local vPlayerToHeal, vPlayerHealth = Find_Lowest_HP()
	if vPlayerToHeal ~= nil
	and vPlayerHealth ~= nil
	then
		-- Raid Specific
		if ccrUnitHasDebuff(vPlayerToHeal, "Strong Ancient Barrier", "any") then vPlayerHealth = 80 end
		if ccrUnitHasDebuff(vPlayerToHeal, "Ancient Barrier", "any") then vPlayerHealth = 60 end
		if ccrUnitHasDebuff(vPlayerToHeal, "Weak Ancient Barrier", "any") then vPlayerHealth = 40 end

		if ccrTargetName() == "Thok the Bloodthirsty"
		and ccrTargetIsCastingSpell("Deafening Screech")
		and ccrTargetHasLeftOnCast(1)
		then
			if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
			return
		end
		----------------

		-- Wait until casting is over 
		if ccrPlayerIsCastingOrChanneling() then return end
		--

		-- CD's in combat
		if IsMounted() == nil then
			if ccrPlayerIsInCombat() then
				-- Use trinkets in combat
				UseTrinkets();

				-- Use Innervate if low on mana
					if ccrPlayerMana() <= 80
					and SpellIsReady("Innervate")
					then
						ccrCast("Innervate", ccrPlayer)
					end

				-- Nature's Vigil
				if SpellIsReady("Nature's Vigil") then ccrCast("Nature's Vigil") end
				-- Force of Nature
				if vPlayerHealth <= 95
				and SpellIsReady("Force of Nature")
				and not ccrPlayerHasBuff("Force of Nature")
				then
					if GroupedPlayersHealth(vPlayerToHeal, 20, 95) >= 3
					then
					ccrCast("Force of Nature", vPlayerToHeal)
					if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
					return
					end
				end
				-- Tranquility with Berserking
				if vPlayerHealth <= 50
				and SpellIsReady("Tranquility")
				and not ccrPlayerIsMoving()
				then
				if GroupedPlayersHealth(vPlayerToHeal, 40, 50) >= 8 then
					if SpellIsReady("Berserking") then ccrCast("Berserking") end
					if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
					ccrCast("Tranquility", vPlayerToHeal)
					if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
					return
					end
				end
			
			end
			if not ccrPlayerHasBuff("Harmony")
			and ccrPlayerIsInCombat()
			and not ccrPlayerIsMoving()
			then
				ccrCast("Regrowth", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end
			--------------------------------- Dispel ---------------------------------
				if SpellIsReady("Nature's Cure") then
					if ccrUnitHasDebuffStacks(vPlayerToHeal, "Shadow Word: Bane", 2, "any")
					or ccrUnitHasDebuff(vPlayerToHeal, "Self-Absorbed")
					or ccrUnitHasDebuffStacks(vPlayerToHeal, "Corrosive Blood", 1, "any")
					then
						ccrCast("Nature's Cure", vPlayerToHeal)
					end
				end
			--------------------------------- LMG \ Clearcasting ---------------------------------
			-- Regrowth at 90%
			if vPlayerHealth <= 90
			and ccrPlayerHasBuff("Clearcasting")
			and SpellIsReady("Regrowth")
			and not ccrPlayerIsMoving()
			then
				ccrCast("Regrowth", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end
			if vPlayerHealth <= 100
			and SpellIsReady("Wild Growth")
			and ccrPlayerHasBuff("Lucidity")
				then
					ccrCast("Wild Growth", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end
			if vPlayerHealth <= 100
			and ccrPlayerHasBuff("Lucidity")
			and not ccrUnitHasMyBuff(vPlayerToHeal, "Rejuvenation")
			and SpellIsReady("Rejuvenation")
				then
				ccrCast("Rejuvenation", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end	
			--------------------------------- Encounter Specific ---------------------------------
			--------------------------------- TANK ---------------------------------
			-- Wild Growth at 90% 6
			if vPlayerHealth <= 95
			and SpellIsReady("Wild Growth")
			then
				if GroupedPlayersHealth(vPlayerToHeal, 20, 90) >= 4
				then
					ccrCast("Wild Growth", vPlayerToHeal)
				return
				end
			end
			-- Healing Touch with Sage Mender
			if vPlayerHealth <= 85
			and ccrPlayerHasBuff("Sage Mender") and ccrPlayerHasBuffStacks("Sage Mender", 5)
			and SpellIsReady("Healing Touch")
			and not ccrPlayerIsMoving()
			then
				ccrCast("Healing Touch", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end
			-- Regrowth at 50%
			if vPlayerHealth <= 50
			and UnitGroupRolesAssigned(vPlayerToHeal) == "TANK"
			and SpellIsReady("Regrowth")
			and not ccrPlayerIsMoving()
			then
				ccrCast("Regrowth", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end
			-- Healing Touch with Nature's Swiftness
			if vPlayerHealth <= 50
			and SpellIsReady("Healing Touch")
			and UnitGroupRolesAssigned(vPlayerToHeal) == "TANK" 
			then
				if SpellIsReady("Nature's Swiftness") then ccrCast("Nature's Swiftness") end
				if ccrPlayerHasBuff("Nature's Swiftness") then
				ccrCast("Healing Touch", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
				end
			end
			-- Lifebloom on Tanks at 100%
			if vPlayerHealth <= 100
			and UnitGroupRolesAssigned(vPlayerToHeal) == "TANK"
			and not ccrUnitHasMyBuff(vPlayerToHeal, "Lifebloom") and not (ccrUnitHasBuffStacksLeft(vPlayerToHeal, "Lifebloom", 2) or ccrUnitHasBuffStacksLeft(vPlayerToHeal, "Lifebloom", 1))
			and SpellIsReady("Lifebloom")
				then
				ccrCast("Lifebloom", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end	

			-- Rejuvenation on Tanks at 100%
			if vPlayerHealth <= 100
			and UnitGroupRolesAssigned(vPlayerToHeal) == "TANK"
			and not ccrUnitHasMyBuff(vPlayerToHeal, "Rejuvenation")
			and SpellIsReady("Rejuvenation")
				then
				ccrCast("Rejuvenation", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end	
			-- Cenarion Ward
			if vPlayerHealth <= 100
			and UnitGroupRolesAssigned(vPlayerToHeal) == "TANK"
			and not ccrUnitHasMyBuff(vPlayerToHeal, "Cenarion Ward")
			and SpellIsReady("Cenarion Ward")
				then
				ccrCast("Cenarion Ward", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end	
			--------------------------------- RAID ---------------------------------
			-- Regrowth at 40%
			if vPlayerHealth <= 40
			and SpellIsReady("Regrowth")
			and not ccrPlayerIsMoving()
			then
				ccrCast("Regrowth", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end
			-- Rejuvenation at 87%
			if vPlayerHealth <= 87
			and not ccrUnitHasMyBuff(vPlayerToHeal, "Rejuvenation")
			then
				ccrCast("Rejuvenation", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
			end
			-- Swiftmend
			if vPlayerHealth <= 90
			and ccrUnitHasMyBuff(vPlayerToHeal, "Rejuvenation")
			then
				if GroupedPlayersHealth(vPlayerToHeal, 20, 95) >= 3 then
				ccrCast("Swiftmend", vPlayerToHeal)
				if vTargetPlayerToHeal then TargetUnit(vPlayerToHeal) end
				return
				end
			end
	    end
    end
end